<?php

namespace App\Http\Controllers\Shop;

use Illuminate\Http\Request;

use DB;
use Auth;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class HomeController extends Controller
{
    /**
     * 显示商铺首页
     * @return \Illuminate\Http\Response
     */
    public function getIndex(Request $request,$id)
    {
        $keywords = $request->input('keywords');
        //获取商品的分类
        $cates = DB::table('cates')->where('pid','!=','0')->get();
        $data = DB::table('shops_details')->join('shops','shops.id','=','shops_details.id')->where('shops_details.id','=',$id)->get()[0];
        //热销商品
        $hot = DB::table('goods')->where('sid','=',$id)->where('hot','=',1)->get();
        //所有上品
        $coupons = DB::table('coupons')->where('sid','=',$id)->get();

        if(empty($keywords)){
            //所有上品
            $goods = DB::table('goods')->where('sid','=',$id)->get(); 
        }else{
            $goods = DB::table('goods')->where('sid','=',$id)->where('spname','like','%'.$keywords.'%')->get();
        }

        //友情链接
        $links = DB::table('links') -> get();
        return view('home.shop.index',['data'=>$data,'hot'=>$hot,'goods'=>$goods,'cates'=>$cates,'links'=>$links,'coupons'=>$coupons]);
    }

    /**
     * 按照分类显示商品
     * @return \Illuminate\Http\Response
     */
    public function getCates($id,$cid)
    {
        //获取商品的分类
        $cates = DB::table('cates')->where('pid','!=','0')->get();
        $data = DB::table('shops_details')->join('shops','shops.id','=','shops_details.id')->where('shops_details.id','=',$id)->get()[0];
        //热销商品
        $hot = DB::table('goods')->where('sid','=',$id)->where('hot','=',1)->get();
        //所有上品
        $goods = DB::table('goods')->where('sid','=',$id)->where('cate','=',$cid)->get();
        return view('home.shop.cates',['data'=>$data,'hot'=>$hot,'goods'=>$goods,'cates'=>$cates]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
